//
//  HaveHandleViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/19.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "HaveHandleViewController.h"

@interface HaveHandleViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic,assign) BOOL isHeadFresh;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;

@end

@implementation HaveHandleViewController

static NSString * cellID = @"cell";
static NSString * MyRepariStateTableViewCellID = @"MyRepariStateTableViewCell";
static NSString * MyRepariContentTableViewCellID = @"MyRepariContentTableViewCell";
static NSString * MyRepariImgTableViewCellID = @"MyRepariImgTableViewCell";
static NSString * MyRepariCancelTableViewCellID = @"MyRepariCancelTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
       self.view .backgroundColor=[UIColor colorWithRed:arc4random_uniform(255.0)/255.0 green:arc4random_uniform(255.0)/255.0 blue:arc4random_uniform(255.0)/255.0 alpha:1.0];
    [self creatTableview];
    [self registCell];
    [self RequestData];
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - 请求数据
-(void)RequestData
{
    WeakSelf;
    NSString * urlStr = [NSString stringWithFormat:@"/rest/wxRecordController/getWxRecordList?pageno=%d&pagesize=10&bxrTel=&bxrName&spJs=0",self.page];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSArray * dataArr = dic[@"data"][@"list"];
        for (NSDictionary * dics in dataArr) {
            RepariModel * model = [RepariModel modelWithDic:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        
    }];
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[MyRepariStateTableViewCell class] forCellReuseIdentifier:MyRepariStateTableViewCellID];
    [self.tableview registerClass:[MyRepariContentTableViewCell class] forCellReuseIdentifier:MyRepariContentTableViewCellID];
    [self.tableview registerClass:[MyRepariImgTableViewCell class] forCellReuseIdentifier:MyRepariImgTableViewCellID];
    [self.tableview registerClass:[MyRepariCancelTableViewCell class] forCellReuseIdentifier:MyRepariCancelTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH - 50) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    line.backgroundColor = SFMColor(239, 239, 244);
    return line;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArr.count == 0) {
        return 0;
    }
    RepariModel * model = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
        return 82;
    }else if (indexPath.row == 1)
    {
        return model.contentHeight;
    }
    else if (indexPath.row == 2)
    {
        NSMutableArray * arr = [NSMutableArray array];
        if (model.bxFj1 != nil) {
            [arr addObject:model.bxFj1];
        }
        if (model.bxFj2 != nil) {
            [arr addObject:model.bxFj2];
        }
        
        if (model.bxFj3 != nil) {
            [arr addObject:model.bxFj3];
        }
        if (arr.count) {
            return 15 + (WIDTH - 70)/4 + 10 * 1 + 13;
        }else
        {
            return 0;
        }
    }else
    {
        return 55;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArr.count == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        return cell;
    }
    RepariModel * model = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
        MyRepariStateTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyRepariStateTableViewCellID];
        [cell sendRepariModel:model];
        cell.stateLabel.text = @"报修已完成";
        cell.stateLabel.textColor = SFMNavBarColor;
        return cell;
    }else if (indexPath.row == 1)
    {
        MyRepariContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyRepariContentTableViewCellID];
        [cell sendRepariModel:model];
        return cell;
    }else if (indexPath.row == 2)
    {
        NSMutableArray * arr = [NSMutableArray array];
        if (model.bxFj1 != nil) {
            [arr addObject:model.bxFj1];
        }
        if (model.bxFj2 != nil) {
            [arr addObject:model.bxFj2];
        }
        
        if (model.bxFj3 != nil) {
            [arr addObject:model.bxFj3];
        }
        if (arr.count) {
            MyRepariImgTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyRepariImgTableViewCellID];
            [cell sendImgArr:arr];
            return cell;
        }else
        {
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            return cell;
        }
    }else
    {
        MyRepariCancelTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyRepariCancelTableViewCellID];
        [cell.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn setTitle:@"报修评价" forState:UIControlStateNormal];
        [cell.btn setTitleColor:SFMWhiteColor forState:UIControlStateNormal];
        [cell.btn setBackgroundColor:SFMNavBarColor];
        cell.btn.tag = indexPath.section;
        return cell;
    }
}

#pragma mark - 报修评价
-(void)btnClick:(UIButton *)btn
{
    RepariModel * model = [self.dataArr objectAtIndex:btn.tag - 1];
    JudgeViewController * ctr = [[JudgeViewController alloc] init];
    ctr.repariID = model.repariID;
    [self.navigationController pushViewController:ctr animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
